//
//  ViewController.m
//  APPCommunication
//
//  Created by 李阳 on 15-7-1.
//  Copyright (c) 2015年 liyang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    
    CGRect frame = CGRectMake(100, 200, 100, 50);
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [self.view addSubview:btn];
    [btn setTitle:@"点我吧！" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor blueColor];
    [btn addTarget:self action:@selector(showItem) forControlEvents:UIControlEventTouchUpInside];
}

-(void)showItem{
    [self showItem:@"12688928896"];
}

- (void)showItem:(NSString *)itemId {
    
    // 构建客户端协议的 URL
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"haixdai://m.haixdai.com/mobile/invest/detail?borrowid=%@", itemId]];
    
    // 判断当前系统是否有安装客户端
    
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        
        // 如果已经安装客户端，就使用客户端打开链接
        
        NSLog(@"~1~");
        
        [[UIApplication sharedApplication] openURL:url];
        
    } else {
        
        // 否则使用 Mobile Safari 或者内嵌 WebView 来显示
        
        NSLog(@"~2~");
        
        url = [NSURL URLWithString:[NSString stringWithFormat:@"http://m.haixdai.com/mobile/invest/detail?borrowid=%@", itemId]];
        
        [[UIApplication sharedApplication] openURL:url];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end